unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, Menus, StdCtrls, ImgList, ExtCtrls, Grids, DBGrids, DBCtrls,
  Buttons;

type
  TfrmMain = class(TForm)
    PageControl: TPageControl;
    tsConnection: TTabSheet;
    tsInterruptSQL: TTabSheet;
    plTop: TPanel;
    memSQL: TMemo;
    Splitter: TSplitter;
    MainMenu: TMainMenu;
    mmiFile: TMenuItem;
    mmiGoToLxC: TMenuItem;
    mmiDelimiter: TMenuItem;
    mmiExit: TMenuItem;
    mmiHelp: TMenuItem;
    mmiAbout: TMenuItem;
    grbParamsMemo: TGroupBox;
    memConnParams: TMemo;
    btnLoadDefParams: TButton;
    btnLoadParams: TButton;
    btnSaveParams: TButton;
    btnConnect: TButton;
    btnDisconnect: TButton;
    labelSQL: TLabel;
    btnExecSQL: TButton;
    btnInterruptSQL: TButton;
    chbUseInterruptSQL: TCheckBox;
    DBNavigator: TDBNavigator;
    btnOpen: TButton;
    panelResults: TPanel;
    DBGrid: TDBGrid;
    labelResults: TLabel;
    procedure mmiAboutClick(Sender: TObject);
    procedure chbUseInterruptSQLClick(Sender: TObject);
    procedure btnInterruptSQLClick(Sender: TObject);
    procedure btnExecSQLClick(Sender: TObject);
    procedure btnLoadParamsClick(Sender: TObject);
    procedure btnLoadDefParamsClick(Sender: TObject);
    procedure btnSaveParamsClick(Sender: TObject);
    procedure btnConnectClick(Sender: TObject);
    procedure btnDisconnectClick(Sender: TObject);
    procedure mmiGoToLxCClick(Sender: TObject);
    procedure mmiExitClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
  private

  public
    procedure InitControls(Connected: Boolean);
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

uses DM, About, ShellAPI, IfxConnection;

{$R *.DFM}

const
  InterruptConfirm = 'Are you sure you want to interrupt sql request?';

procedure TfrmMain.InitControls(Connected: Boolean);
begin
  // Connection tab
  btnConnect.Enabled := not Connected;
  btnDisconnect.Enabled := Connected;

  // Execute/Interrupt SQL tab
  btnOpen.Enabled := Connected;
  btnExecSQL.Enabled := Connected;
  btnInterruptSQL.Enabled := Connected and chbUseInterruptSQL.Checked;
  chbUseInterruptSQL.Enabled := Connected;
end;

procedure TfrmMain.FormShow(Sender: TObject);
var
  DatabaseIndex: Integer;
begin
  TIfxConnection.GetInformixEnvironment(memConnParams.Lines);
  DatabaseIndex := memConnParams.Lines.IndexOfName(szDATABASE);
  if DatabaseIndex <> -1 then
    memConnParams.Lines.Delete(DatabaseIndex);
  memConnParams.Lines.Insert(0, szDATABASE + '=stores7');
end;

procedure TfrmMain.mmiGoToLxCClick(Sender: TObject);
begin
  ShellExecute(Handle, PChar('open'), PChar('http://www.luxena.com'), nil, nil, SW_SHOW);
end;

procedure TfrmMain.mmiExitClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.mmiAboutClick(Sender: TObject);
var
  AboutBox: TfrmAboutBox;
begin
  AboutBox := TfrmAboutBox.Create(Self);
  try
    AboutBox.ShowModal;
  finally
    AboutBox.Free;
  end;
end;

procedure TfrmMain.chbUseInterruptSQLClick(Sender: TObject);
begin
  frmDM.IfxConnection.UseTimeOut := chbUseInterruptSQL.Checked;
  btnInterruptSQL.Enabled := chbUseInterruptSQL.Checked;
end;

procedure TfrmMain.btnOpenClick(Sender: TObject);
begin
  frmDM.IfxQuery.SQL.Assign(memSQL.Lines);
  frmDM.IfxQuery.Open;
end;

procedure TfrmMain.btnExecSQLClick(Sender: TObject);
begin
  frmDM.IfxQuery.Close;

  frmDM.IfxQuery.SQL.Assign(memSQL.Lines);
  frmDM.IfxQuery.ExecSQL;
end;

procedure TfrmMain.btnInterruptSQLClick(Sender: TObject);
begin
  if MessageDlg(InterruptConfirm, mtConfirmation, [mbYes, mbNo], 0) = mrYes then
    frmDM.IfxConnection.InterruptSQL;
end;

procedure TfrmMain.btnLoadParamsClick(Sender: TObject);
begin
  frmDM.OpenDialog.InitialDir := GetCurrentDir;

  if frmDM.OpenDialog.Execute then
    memConnParams.Lines.LoadFromFile(frmDM.OpenDialog.FileName);
end;

procedure TfrmMain.btnLoadDefParamsClick(Sender: TObject);
begin
  frmDM.IfxConnection.GetInformixEnvironment(memConnParams.Lines);
end;

procedure TfrmMain.btnSaveParamsClick(Sender: TObject);
begin
  frmDM.SaveDialog.InitialDir := GetCurrentDir;

  if frmDM.SaveDialog.Execute then
    memConnParams.Lines.SaveToFile(frmDM.SaveDialog.FileName);
end;

procedure TfrmMain.btnConnectClick(Sender: TObject);
begin
  frmDM.IfxConnection.Params.Assign(memConnParams.Lines);
  frmDM.IfxConnection.Open;
end;

procedure TfrmMain.btnDisconnectClick(Sender: TObject);
begin
  frmDM.IfxConnection.Close;
end;


end.

